home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Testing & Debugging / General tools / Audit app & dcmd / Src / DisplayAuditFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-17  |  4.3 KB  |  168 lines  |  [TEXT/KAHL]

  1. /*                                DisplayAuditFile.c                                */
  2. /*
  3.  * DisplayAuditFile.c
  4.  * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
  5.  * Output file writer for the Audit library.
  6.  * Edit History
  7.  *    93.01.14 MM        Removed Think C stdio dependencies. Added  Standard Fil
  8.  *                    dialog with additional dialog items to allow defining the
  9.  *                    audit selector.
  10.  *    93.01.19 MM        MPW \r is the wrong character. Force <returns> on both MPW and
  11.  *                    Think, because this is what the Mac file system requires for
  12.  *                    end of line. (Change is in DisplayAuditFile.c)
  13.  *    93.07.09 MM        Reformatted. CloseOutputFile now takes file refNum, volume
  14.  *                    refNum and filename parameters, rather than a SFReply.
  15.  *    93.08.10 MM        This could be replaced by the code already in the LogManager.
  16.  */
  17. #include "DisplayAudit.h"
  18. #define kEndOfLine                0x0D    /* Return            */
  19.  
  20.  
  21. /*
  22.  * File Output
  23.  * Open/create a file to write the audit information. reply.good is TRUE on
  24.  * success, FALSE if the user cancels the SFPutFile. Serious errors fail via the
  25.  * failure mechanism. The file is TEXT type. The creator is TeachText. On success:
  26.  *         reply.vRefNum        The volume id.
  27.  *         fileRefNum            The file id.
  28.  */
  29. void
  30. PromptAndCreateAuditOutputFile(
  31.         ConstStr255Param                promptString,
  32.         ConstStr255Param                defaultFileName,
  33.         OSType                            creator,
  34.         short                            *fileRefNum,
  35.         short                            *volumeRefNum,
  36.         Str63                            fileName
  37.     )
  38. {
  39.         Point                            where;
  40.         DialogTHndl                        dialog;
  41.         Rect                            box;
  42.         SFReply                            reply;
  43.         
  44.         /*
  45.          * Center the dialog
  46.          */
  47.         dialog = (DialogTHndl) GetResource('DLOG', putDlgID);
  48.         if (dialog == NULL)                                /* No resource!            */
  49.             SetPt(&where, 80, 80);
  50.         else {
  51.             box = (**dialog).boundsRect;                /* Dialog shape            */
  52.             ReleaseResource((Handle) dialog);
  53.             where.h =
  54.                 (width(qd.thePort->portRect) - width(box)) / 2;
  55.             where.v = 
  56.                 ((height(qd.thePort->portRect) - GetMBarHeight()) / 3)
  57.                 + GetMBarHeight();
  58.         }
  59.         SFPutFile(where, promptString, defaultFileName, NULL, &reply);
  60.         if (reply.good == FALSE)
  61.             *fileRefNum = 0;
  62.         else {
  63.             FailOSErr(
  64.                 CreateOutputFile(creator, reply.fName, reply.vRefNum, fileRefNum),
  65.                 kErrCreateOutputFile
  66.             );
  67.             pstrcpy(fileName, reply.fName);
  68.             *volumeRefNum = reply.vRefNum;
  69.         }
  70. }
  71.  
  72. /*
  73.  * Create an output file. Return noErr on success. On success, fileRefNum has the
  74.  * refNum. On Failure, it has zero.
  75.  */
  76. OSErr
  77. CreateOutputFile(
  78.         OSType                            creator,
  79.         ConstStr255Param                fileName,
  80.         short                            vRefNum,
  81.         short                            *fileRefNum
  82.     )
  83. {
  84.         OSErr                            status;
  85.         
  86.         /*
  87.          * Create the file, elmininating any duplicate.
  88.          */
  89.         SetCursor(*GetCursor(watchCursor));
  90.         status = Create(fileName, vRefNum, creator, 'TEXT');
  91.         if (status == dupFNErr)     {                    /* Exists already?        */
  92.             status = FSDelete(fileName, vRefNum);
  93.             if (status == noErr)
  94.                 status = Create(fileName, vRefNum, creator, 'TEXT');
  95.         }
  96.         if (status == noErr)
  97.             status = FSOpen(fileName, vRefNum, fileRefNum);
  98.         if (status != noErr)
  99.             *fileRefNum = 0;
  100.         InitCursor();
  101.         return (status);
  102. }
  103.  
  104. /*
  105.  * Write one line of text to the output file. Fail on errors.
  106.  */
  107. void
  108. WriteAuditOutputLine(
  109.         ConstStr255Param                theText,
  110.         short                            fileRefNum
  111.     )
  112. {
  113.         long                            textLength;
  114.         static char                        gEndOfLine[1] = { kEndOfLine };
  115.  
  116.         textLength = theText[0];
  117.         if (textLength > 0) {
  118.             FailOSErr(
  119.                 FSWrite(fileRefNum, &textLength, &theText[1]),
  120.                 kErrWriteOutputFile
  121.             );
  122.         }
  123.         textLength = 1;
  124.         FailOSErr(
  125.             FSWrite(fileRefNum, &textLength, gEndOfLine),
  126.             kErrWriteOutputFile
  127.         );
  128. }
  129.  
  130. /*
  131.  * Close the Audit output file. If status == noErr, the file is closed normally,
  132.  * Otherwise, the program cleans up after an error. Errors return via the Failure
  133.  * mechanism. (Note: this includes a call with status != noErr.) The I/O model is
  134.  * as follows:
  135.  *        TRY {
  136.  *            PromptAndCreateAuditOutputFile(...);
  137.  *            WriteAuditOutputLine(...);
  138.  *            CloseAuditOutputFile(noErr, ...);
  139.  *        }
  140.  *        CATCH {
  141.  *            CloseAuditOutputFile(STATUS, ...);
  142.  *        }
  143.  *        ENDTRY;
  144.  */
  145. void
  146. CloseAuditOutputFile(
  147.         OSErr                            status,
  148.         short                            fileRefNum,
  149.         short                            volumeRefNum,
  150.         ConstStr255Param                fileName
  151.     )
  152. {
  153.         if (status == noErr) {
  154.             TRY {
  155.                 FailOSErr(FSClose(fileRefNum), kNoMessage);
  156.                 FailOSErr(FlushVol(NULL, volumeRefNum), kNoMessage);
  157.             }
  158.             CATCH {
  159.                 status = STATUS;
  160.                 NO_PROPAGATE;
  161.             }
  162.             ENDTRY;
  163.         }
  164.         if (status != noErr)
  165.             (void) FSDelete(fileName, volumeRefNum);
  166.         FailOSErr(status, kErrCloseOutputFile);
  167. }
  168.